﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Website.ClientService;
using MobileSales.Website.Infrastructure;
using MobileSales.Website.Infrastructure.Messaging;
using MobileSales.Model.Clients;
using System.ServiceModel;
using MobileSales.Model.Exceptions;
using MobileSales.Common.Logger;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class ClientGroupsController : ControllerBase
    {
        #region Index
        //
        // GET: /ClientGroups/

        public ActionResult Index()
        {
            ClientServiceClient clientService = new ClientServiceClient();
            clientService.AddValidationToken(this.Token);
            ClientGroup[] groups = clientService.GetClientGroupList();
            clientService.Close();
            if (groups == null)
                this.SetMessage(ErrorStrings.ErrorClientGroupList, MessageType.Error);

            return View(groups);
        } 
        #endregion

        #region Create
        //
        // GET: /ClientGroups/Create

        public ActionResult Create()
        {
            return View();
        }  

        //
        // POST: /ClientGroups/Create

        [HttpPost]
        public ActionResult Create(ClientGroup group)
        {
            try
            {
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.StoreClientGroup(group);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientGroupCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientGroupCreating, MessageType.Error);
            }
            return View(group);
        }
        #endregion

        #region Edit
        //
        // GET: /ClientGroups/Edit/5

        public ActionResult Edit(int id)
        {
            ClientServiceClient clientService = new ClientServiceClient();
            clientService.AddValidationToken(this.Token);
            ClientGroup group = clientService.GetClientGroup(id);
            clientService.Close();
            if (group == null)
            {
                this.SetMessage(ErrorStrings.ErrorClientGroup, MessageType.Error);
                return RedirectToAction("Index");
            }
            return View(group);
        }

        //
        // POST: /ClientGroups/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, ClientGroup group)
        {
            try
            {
                group.Id = id;
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.StoreClientGroup(group);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientGroupUpdated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientGroupUpdating, MessageType.Error);
            }
            return View(group);
        } 
        #endregion

        #region Delete
        //
        // GET: /ClientGroups/Delete/5

        public ActionResult Delete(int id)
        {
            return Edit(id);
        }

        //
        // POST: /ClientGroups/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                ClientServiceClient clientService = new ClientServiceClient();
                clientService.AddValidationToken(this.Token);
                clientService.DeleteClientGroup(id);
                clientService.Close();
                this.SetMessage(InfoStrings.ClientGroupDeleted, MessageType.Info);
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorClientGroupDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion
    }
}
